//
//  MYXEmptyView.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/9.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXEmptyView.h"
#import "JXTColorHelper.h"


@implementation MYXEmptyView

{
    UIImageView *_indicatorImageView;
    //    UILabel *_indicatorTextView;
    UIButton *_operationButton;
    
    ButtonTapBlock _block;
}

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)image text:(NSString *)text ButtonText:(NSString *)buttonText buttonTapped:(ButtonTapBlock)buttonTapped
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        if (image != nil) {
            _indicatorImageView = [[UIImageView alloc] initWithImage:image];
            _indicatorImageView.frame = CGRectMake((frame.size.width- image.size.width)/2.0, frame.size.height/3.0 - 40, image.size.width,  image.size.height);
            [self addSubview:_indicatorImageView];
        }
        if (text != nil && ![text isEqualToString:@""]) {
            _indicatorTextView = [UILabel new];
            _indicatorTextView.numberOfLines = 1;
            _indicatorTextView.textColor = RGBACOLOR(169, 169, 169, 1.0);
            _indicatorTextView.font = [UIFont systemFontOfSize:14];
            _indicatorTextView.textAlignment = NSTextAlignmentCenter;
            _indicatorTextView.text = text;
            if (_indicatorImageView == nil) {
                _indicatorTextView.frame = CGRectMake(0, 0, WIDTH, frame.size.height);
            }
            else {
                //_indicatorTextView.frame = CGRectMake(0, frame.size.height/3.0+80, JXTScreenWidth, 20);
                _indicatorTextView.frame = CGRectMake(0, _indicatorImageView.frame.origin.y+_indicatorImageView.frame.size.height+20, WIDTH, 20);
            }
            [self addSubview:_indicatorTextView];
        }
        if (buttonText!= nil && ![buttonText isEqualToString:@""]) {
            _operationButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _operationButton.layer.masksToBounds = YES;
            _operationButton.layer.cornerRadius = 4;
            _operationButton.layer.borderColor = [RGBACOLOR(230, 230, 230, 1.0) CGColor];
            _operationButton.layer.borderWidth = 1;
            _operationButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [_operationButton setBackgroundImage:[JXTColorHelper imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [_operationButton setBackgroundImage:[JXTColorHelper imageWithColor:RGBACOLOR(243, 244, 244, 1.0)] forState:UIControlStateHighlighted];
            [_operationButton setTitleColor:RGBACOLOR(85, 85, 85, 1.0) forState:UIControlStateNormal];
            [_operationButton setTitle:buttonText forState:UIControlStateNormal];
            _operationButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
            CGRect textFrame = _indicatorTextView.frame;
            //_operationButton.frame = CGRectMake(frame.size.width/2.0 - 50, textFrame.origin.y+textFrame.size.height + 15, 100, 40);
            _operationButton.frame = CGRectMake(frame.size.width/2.0 - 50, textFrame.origin.y+textFrame.size.height + 15, 100, 40);
            [self addSubview:_operationButton];
            if (buttonTapped != nil) {
                _block = buttonTapped;
            }
            [_operationButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame withIndicator:(BOOL)withIndicator text:(NSString *)text ButtonText:(NSString *)buttonText buttonTapped:(ButtonTapBlock)buttonTapped withBackgroundColor:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = color;
        if (withIndicator) {
            UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            indicatorView.hidesWhenStopped = YES;
            indicatorView.center = CGPointMake(frame.size.width/2.0, frame.size.height/2.0);
            [self addSubview:indicatorView];
            [indicatorView startAnimating];
        }
        if (![text isEqualToString:@""]) {
            _indicatorTextView = [UILabel new];
            _indicatorTextView.numberOfLines = 1;
            _indicatorTextView.textColor = RGBACOLOR(169, 169, 169, 1.0);
            _indicatorTextView.font = [UIFont systemFontOfSize:14];
            _indicatorTextView.textAlignment = NSTextAlignmentCenter;
            _indicatorTextView.text = text;
            _indicatorTextView.frame = CGRectMake(0, frame.size.height/2.0 + 5, WIDTH, 50);
            [self addSubview:_indicatorTextView];
        }
        if (![buttonText isEqualToString:@""]) {
            _operationButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _operationButton.layer.masksToBounds = YES;
            _operationButton.layer.cornerRadius = 4;
            _operationButton.layer.borderColor = [RGBACOLOR(230, 230, 230, 1.0) CGColor];
            _operationButton.layer.borderWidth = 1;
            _operationButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [_operationButton setBackgroundImage:[JXTColorHelper imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [_operationButton setBackgroundImage:[JXTColorHelper imageWithColor:RGBACOLOR(243, 244, 244, 1.0)] forState:UIControlStateHighlighted];
            [_operationButton setTitleColor:RGBACOLOR(85, 85, 85, 1.0) forState:UIControlStateNormal];
            [_operationButton setTitle:buttonText forState:UIControlStateNormal];
            _operationButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
            _operationButton.frame = CGRectMake(frame.size.width/2.0 - 50, 60, 80, 30);
            [self addSubview:_operationButton];
            if (buttonTapped != nil) {
                _block = buttonTapped;
            }
            [_operationButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    return self;
}

- (void)buttonEvent
{
    _block();
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
